﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using ICDA.Models.Entity;
using ICDA.Models;
using System.Diagnostics;

namespace ICDA.Repositories.ICDA
{
    //Reference : http://www.codeproject.com/KB/database/ImplRepositoryPatternEF.aspx
    public class Repository :IRepository, IDisposable
    {
        private ICDAEntities context;

        public IObjectSet<UserTable> users
        {
            get
            {
                return context.CreateObjectSet<UserTable>();
            }
        }
        public IObjectSet<LeaveTable> leavetable
        {
            get
            {
                return context.CreateObjectSet<LeaveTable>();
            }
        }
        public IObjectSet<ActivityTable> activity
        {
            get
            {
                return context.CreateObjectSet<ActivityTable>();
            }
        }

        public Repository()
        {
            if (context == null)
            {
                //DbConnection conn = new EntityConnection(@"metadata=res://*/Entity.ICDA.csdl|res://*/Entity.ICDA.ssdl|res://*/Entity.ICDA.msl;provider=System.Data.SqlClient;provider connection string=&quot;Initial Catalog=ICDA;Integrated Security=True;MultipleActiveResultSets=True&quot;'");

                //DbConnection profiledConnection = new EFProfiledDbConnection(conn, MiniProfiler.Current);
                //context = new ICDAEntities(profiledConnection as EntityConnection);
                context = new ICDAEntities();
            }
        }

        public void Save<TModel>(TModel data)
        {
            context.AddObject(data.GetType().Name, data);
        }

        public bool AddTask(ActivityTable act)
        {
            try
            {
                context.AddToActivityTables(act);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        public bool UpdateUser(UserTable usr)
        {
            try
            {
                context.AddToUserTables(usr);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public bool AddLeave(LeaveTable leave)
        {
            try
            {
                context.AddToLeaveTables(leave);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        public bool DeleteLeave(LeaveTable leave)
        {
            try
            {
                context.DeleteObject(leave);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        public bool ChangePassword(UserTable usr)
        {
            try
            {
                context.AddToUserTables(usr);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public bool DeleteTask(ActivityTable act)
        {
            try
            {
                context.DeleteObject(act);
                return true;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        public void Save<TModel>(List<TModel> data)
        {
            foreach (TModel instance in data)
            {
                context.AddObject(instance.GetType().Name, instance);
            }
        }

        public void Delete<TModel>(TModel data)
        {
            context.DeleteObject(data);
        }

        public ObjectQuery<TModel> Select<TModel>(string query, params ObjectParameter[] parameters)
        {
            ObjectQuery<TModel> result = context.CreateQuery<TModel>(query, parameters);
            return result;
        }

        public bool SaveChanges()
        {
            bool success = false;
            try
            {
                context.SaveChanges();
                success = true;
            }
            catch (Exception ex)
            {
                throw ex;
                //success = false;
            }
            return success;
        }

        public void Dispose()
        {
            if (null != context)
            {
                context.Dispose();
            }
        }
        
    }
}